About 5591 letters

About 28 minutes

#Python's loop statement

When writing programs, we often need to execute the same code repeatedly.

For example, in the game, a continuous healing skill is released, and the effect is to continuously restore health over a period of time, and the total recovery is ten times.

This is obviously inappropriate:

hp:int = 10 # treat hp += 1 hp += 1 hp += 1 hp += 1 hp += 1 hp += 1 hp += 1 hp += 1 hp += 1 hp += 1

At this time, you need to use a loop to simplify the code. Python has two loop syntaxes, controlled by while and for.

#while

The structure of a while loop is as follows:

# As long as the `loop-condition` is `True`, the `loop-body` will be executed repeatedly # until the `loop-condition` becomes `False`, then the loop ends and the subsequent code continues to execute. while loop-condition: loop-body
G start Start condition loop-condition start->condition loop_body loop-body condition->loop_body yes end End condition->end no loop_body->condition

For example:

hp:int = 10 times:int = 10 # treat while times > 0: print("Treat 1 HP, current HP is", hp) hp += 1 times -= 1 print("Final HP is", hp)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#for

The structure of a for loop is as follows:

# Take out values ​​one by one from the `iterable-object`, assign them to the `loop-variable` and execute the `loop-body` # until all the values ​​of the `iterable-object` are taken out, end the loop, and continue to execute the following code. for loop-variable in iterable-object: loop-body

For example:

hp:int = 10 # treat for _ in range(10): print("Treat 1 HP, current HP is", hp) hp += 1 print("Final HP is", hp)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

  • Here range is a built-in function that returns an iterable object, which gets 0 to n-1 in turn, here 0 to 9.
  • Since we only need to loop 10 times and do not need the iterative value, we use the special variable _ to receive it.

The range function is very commonly used. It has three usages:

range(stop) # Iterate over the objects from 0 to stop, excluding stop itself, and increment by 1 each time range(start, stop) # Iterate over the objects from start to stop, excluding stop itself, and increment by 1 each time range(start, stop, step) # The iteration object gets from start to stop in sequence, excluding stop itself, and increments step each time

For example:

for num in range(10): print(num, end=", ") # Replace the default line break at the end with the end parameter print("") # Print an empty string, with a default line break at the end for num in range(5, 10): print(num, end=", ") print("") for num in range(5, 10, 3): print(num, end=", ") print("")

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#break & continue

There are two very common operations in a loop, break and continue.

  • break ends the entire loop early
  • continue ends the current loop and enters the next loop.

For example, our health limit is 15 points. If the health limit has been reached, we should not continue to heal.

You can use break to end the healing when the health limit is reached:

MAX_HP:int = 15 hp:int = 10 # treat for _ in range(10): print("Treat 1 HP, current HP is", hp) if hp >= MAX_HP: # If the HP is full, end treatment break hp += 1 print("Final HP is", hp)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

In this way, if the player takes damage during the duration, the healing skill will end early and cannot be restored.

Another way is to skip the recovery by continue when the health limit is reached:

MAX_HP:int = 15 hp:int = 10 # treat for _ in range(10): print("Treat 1 HP, current HP is", hp) if hp >= MAX_HP: # If the HP is full, skip this time continue hp += 1 print("Final HP is", hp)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

This way, if the player would take damage during the duration, the healing skill will restore health on the next regen.

#else

Python allows you to put an else after a loop, and its code block will be executed when the loop ends, but the else code block will not be executed when the loop ends with break.
The else code block will also be executed if the loop condition is False from the beginning (or the iterable object is empty).

while loop-condition: loop-body else: code-block
for loop-variable in iterable-object: loop-body else: code-block

#Practise

Please implement the function to determine whether a positive integer is prime and get the input through input.

  • Special rule that 1 is not a prime number.
  • A positive integer that can only be divided by 1 and itself is a prime number, otherwise it is not.
number:int = int(input("Please enter an integer:")) is_prime:bool = False # Please implement the code here and change is_prime to True if number is prime if is_prime: print(number, "is a prime number") else: print(number, "is not a prime number")

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Created in 5/15/2025

Updated in 5/21/2025